//
//  BCMineController.swift
//  Blockfolio
//
//  Created by Maybe on 09/05/2021.
//

import UIKit
import Charts

class BCMineController: BCBaseController {
    var pieChartView: PieChartView = PieChartView()
    var data: PieChartData = PieChartData()
    override func viewDidLoad() {
        
        addPieChartView()
       
        setPieChartViewStyle()
        
        loadData()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
//        self.pieChartView.center = self.view.center
    }

   
}

extension BCMineController {
    
    
    //添加
    func addPieChartView(){
        pieChartView.backgroundColor = UIColor.white
//        pieChartView.frame.size = CGSize.init(width: Screen_width - 80, height: 300)
        pieChartView.frame = CGRect.init(x: 40, y: 20, width: Screen_width - 80, height: 300)

        pieChartView.delegate = self
        self.view.addSubview(pieChartView)
    }
    //设置样式
    func setPieChartViewStyle(){
        pieChartView.setExtraOffsets(left: 20, top: 20, right: 20, bottom: 20)
        pieChartView.usePercentValuesEnabled = true
        pieChartView.dragDecelerationEnabled = true
        pieChartView.drawSlicesUnderHoleEnabled = true  //显示区块文本
        
        pieChartView.drawHoleEnabled = true
        pieChartView.holeRadiusPercent = 0.6
        pieChartView.holeColor = .white
        pieChartView.transparentCircleRadiusPercent = 0.5
        pieChartView.transparentCircleColor = .white
        
        if pieChartView.isDrawHoleEnabled == true {
            pieChartView.drawCenterTextEnabled = true
//            pieChartView.centerText = "资产分布"
            var att:[NSAttributedString.Key : AnyObject] = [:]
            att[.font] = UIFont.boldSystemFont(ofSize: 18)
            att[.foregroundColor] = Color_Title
            let centerText: NSMutableAttributedString = NSMutableAttributedString.init(string: "资产分布", attributes: att)
            pieChartView.centerAttributedText = centerText
        }
        
        pieChartView.chartDescription?.text = ""
        pieChartView.chartDescription?.font = .systemFont(ofSize: 12)
        pieChartView.chartDescription?.textColor = Color_Title
        pieChartView.legend.enabled = false
        
//        let legend = pieChartView.legend
//        legend.maxSizePercent = 1
//        legend.formToTextSpace = 2 //间隔
//        legend.font = .systemFont(ofSize: 6)
//        legend.textColor = .red
//        legend.form = Legend.Form.circle
//        legend.horizontalAlignment = Legend.HorizontalAlignment.center
//        legend.verticalAlignment = Legend.VerticalAlignment.top
//        legend.orientation = Legend.Orientation.horizontal //水平布局
//        legend.formSize = 12
        
    }
    
    func loadData(){
        let count = 5
        let dataEntries:NSMutableArray = NSMutableArray.init()
    
        for i in 0 ..< count {
            let value: Double = Double(arc4random_uniform(UInt32(200)))
            let  entry: PieChartDataEntry = PieChartDataEntry.init(value: value, label: "Coin\(i)")
            dataEntries.add(entry)
        }
        
        let dataSet:PieChartDataSet = PieChartDataSet.init(entries: dataEntries as? [ChartDataEntry], label: "xxxx")
        dataSet.drawIconsEnabled = false
        dataSet.sliceSpace = 0
        dataSet.selectionShift = 10 //选择区域时，放大的半径
        dataSet.highlightEnabled = true
        dataSet.setColors(ChartColorTemplates.material(),alpha: 1)
        dataSet.xValuePosition = PieChartDataSet.ValuePosition.insideSlice
        
        let dataSets: NSMutableArray = NSMutableArray.init()
        dataSets.add(dataSet)
        
        //数据格式化
        let formatter: NumberFormatter = NumberFormatter.init()
        formatter.numberStyle = NumberFormatter.Style.percent
        formatter.maximumFractionDigits = 0
        formatter.multiplier = 1
        
        let barChartData: PieChartData = PieChartData.init(dataSets: (dataSets as! [IChartDataSet]))
        
        let resultForma: DefaultValueFormatter = DefaultValueFormatter.init(formatter: formatter)
        barChartData.setValueFormatter(resultForma)
        barChartData.setValueFont(.systemFont(ofSize: 12))
        barChartData.setValueTextColor(.white)
        pieChartView.data = barChartData
        pieChartView.animate(xAxisDuration: 1, easingOption: ChartEasingOption.easeInExpo)
        
    }
    
}

extension BCMineController: ChartViewDelegate {
    func selectChartView(_ chartView: ChartViewBase,entry: ChartDataEntry, highlight: Highlight){
        BClog("选中了\(entry)")
    }
    
    func noSelectChartView(_ charView: ChartViewBase){
        BClog("取消选择")
        
        //重新渲染
//        charView.data?.notifyDataChanged()
//        charView.notifyDataSetChanged()
    }
    
    func scaleChartView(_ chartView:ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
        BClog("捏合了\(scaleX)")
    }
    
    func translateChartView(_ chartView: ChartViewBase, tX:CGFloat, tY: CGFloat)  {
        BClog("拖拽了\(tX)")
    }
    
}
